package simplenotes;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.Properties;

public class Vars {

    // Version
    private static String Version = "Simple notes 0.70-00";

    public static String getVersion() {
        return Version;
    }
    //
    // Options
    /*
     * DataFile - notes data file name.
     *
     */
    public static Properties opts = new Properties();
    public static String OptsFileName = "";

    public static void SaveOptions() {
        try {
            opts.storeToXML(new FileOutputStream(OptsFileName, false), null);
        } catch (IOException ex) {
            System.out.println("Vars.SaveProperties(): " + ex.toString());
        }
    }

    public static void LoadOptions() {
        try {
            opts.loadFromXML(new FileInputStream(OptsFileName));
        } catch (IOException ex) {
            System.out.println("Vars.LoadProperties(): " + ex.toString());
        }
    }
    //
    // Task list
    //
    public static ArrayList<TaskObj> tasklist = new ArrayList<TaskObj>();

    public static void SaveTaskList() {
        SimpleDateFormat sdf = new SimpleDateFormat("dd.MM.yyyy HH:mm:ss");
        BufferedWriter bw = null;
        try {
            bw = new BufferedWriter(
                    new OutputStreamWriter(
                    new FileOutputStream(opts.getProperty("DataFile")), "UTF-8"));
        } catch (IOException ex) {
            System.out.println("Vars.SaveTaskList(): " + ex.toString());
        }

        try {
            bw.write("Notes file. " + sdf.format(Calendar.getInstance().getTime()));
            bw.newLine();
            bw.write("===============================");
            bw.newLine();

            Long l;
            for (TaskObj ta : tasklist) {
                l = ta.getTaskID();
                bw.write(l.toString());
                bw.newLine();

                l = ta.getCreationTime().getTimeInMillis();
                bw.write(l.toString());
                bw.newLine();

                l = ta.getFinishTime().getTimeInMillis();
                bw.write(l.toString());
                bw.newLine();

                bw.write(getHexString(ta.getTaskText()));
                bw.newLine();

                bw.write(ta.getDone().toString());
                bw.newLine();

                bw.write(ta.getTekuchka().toString());
                bw.newLine();

                l = ta.getChangedTime().getTimeInMillis();
                bw.write(l.toString());
                bw.newLine();

                bw.write("===============================");
                bw.newLine();
            }

        } catch (IOException ex) {
            System.out.println("Vars.SaveTaskList(): " + ex.toString());
        }

        try {
            bw.close();
        } catch (IOException ex) {
            System.out.println("Vars.SaveTaskList(): " + ex.toString());
        }
    }

    public static void LoadTaskList() {
        BufferedReader br = null;
        String line = "";

        try {
            br = new BufferedReader(
                    new InputStreamReader(
                    new FileInputStream(opts.getProperty("DataFile")), "UTF-8"));
        } catch (UnsupportedEncodingException ex) {
            System.out.println("Vars.LoadTaskList(): " + ex.toString());
        } catch (FileNotFoundException ex) {
            System.out.println("Vars.LoadTaskList(): " + ex.toString());
        }

        if (br == null) {
            return;
        }

        tasklist.clear();

        try {
            line = br.readLine();
            line = br.readLine();

            TaskObj ta;
            Long l = new Long(0);
            Long tid;
            Calendar cs;
            Calendar cf;
            String st;
            Boolean bd;
            Boolean tek;
            Calendar chtime;

            while ((line = br.readLine()) != null) {
                if (line.equals("===============================")) {
                    continue;
                }

                tid = Long.valueOf(line);

                line = br.readLine();
                l = Long.valueOf(line);
                cs = new GregorianCalendar();
                cs.setTimeInMillis(l);

                line = br.readLine();
                l = Long.valueOf(line);
                cf = new GregorianCalendar();
                cf.setTimeInMillis(l);

                line = br.readLine();
                st = new String(line);
                st = getStringFromHex(st);

                line = br.readLine();
                bd = new Boolean(line);

                line = br.readLine();
                tek = new Boolean(line);

                line = br.readLine();
                l = Long.valueOf(line);
                chtime = new GregorianCalendar();
                chtime.setTimeInMillis(l);

                ta = new TaskObj(tid, cs, cf, st, bd, tek, chtime);
                tasklist.add(ta);
            }
        } catch (IOException ex) {
            System.out.println("Vars.LoadTaskList(): " + ex.toString());
        }

        try {
            br.close();
        } catch (IOException ex) {
            System.out.println("Vars.LoadTaskList(): " + ex.toString());
        }
    }

    public static String getHexString(String str) {
        String hexstr = "";
        int i = 0;
        while (i < str.length()) {
            Integer ic = str.codePointAt(i);
            hexstr = hexstr + Integer.toHexString(ic);
            hexstr = hexstr + "|";
            i++;
        }
        return hexstr;
    }

    public static String getStringFromHex(String hexstr) {
        String str = "";
        String s = "";
        Integer ci = 0;
        char c = (char) 0;
        int i = 0;
        while (i < hexstr.length()) {
            if (!hexstr.substring(i, i + 1).equals("|")) {
                s = s + hexstr.substring(i, i + 1);
                i++;
            } else {
                ci = Integer.parseInt(s, 16);
                c = (char) ci.intValue();
                str = str + c;
                s = "";
                i++;
            }
        }
        return str;
    }
}
